import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import {Modal} from 'antd'
import { convertContent } from '../constant/lang-util'

export const refreshMyProgramList = myProgramList => ({
  type: 'REFRESH_MY_PROGRAM_LIST',
  myProgramList
})

export const refreshTutoredProgramList = tutoredProgramList => ({
  type: 'REFRESH_TUTORED_PROGRAM_LIST',
  tutoredProgramList
})

export const refreshTopicsTasks = topicsTasks => ({
  type: 'REFRESH_TOPICS_TASKS',
  topicsTasks
})
export const refreshProgram = program => ({
  type: 'REFRESH_PROGRAM',
  program
})

export const refreshLeaderBoard = leaderBoard => ({
  type: 'LEADER_BOARD',
  leaderBoard
})

export const getMyPrograms = (page = 1, pageSize = 8, callback = null) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/student?page=${page}&pageSize=${pageSize}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshMyProgramList(res.body))
        if (callback) {
          callback()
        }
      }
    })()
  }
}

export const getTutoredPrograms = (page = 1, pageSize = 8, callback = null) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/tutored?page=${--page}&pageSize=${pageSize}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTutoredProgramList(res.body))
        if (callback) {
          callback()
        }
      }
    })()
  }
}

export const getProgramIntroduction = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}/introductions`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshProgram(res.body))
      }
    })()
  }
}

export const getTopicTasks = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}/tasks`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTopicsTasks(res.body))
      }
    })()
  }
}

export const getLeaderBoards = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}/leaderBoard`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshLeaderBoard(res.body))
      }
    })()
  }
}
export const getStudentsByProgramId = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}/students`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_STUDENTS',
          data: res.body
        })
      }
    })()
  }
}
export const followStudentByInviteLink = (code) => {
  return () => {
    (async () => {
      const res = await request.post(`../api/programs/followers/links?code=${code}`)
      if (res.status === HTTP_CODE.CREATED) {
        Modal.success({
          content: convertContent('添加成功')
        })
      } else {
        Modal.error({
          content: 'Error.( Please check the format or you have already been followed )'
        })
      }
    })()
  }
}
